// pages/noteEdit/noteEdit.js
import request from '../../utils/request'
import {
  toast
} from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    actions: [{
      name: '美食'
    }, {
      name: '旅行'
    }, {
      name: '汽车'
    }, {
      name: '时尚'
    }, {
      name: '科技'
    }],
    noteContent: "",
    noteTitle: "",
    noteImage: "",
    noteType: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  noteContent(e) {
    this.setData({
      noteContent: e.detail.html
    });
  },
  noteTitle(e) {
    this.setData({
      noteTitle: e.detail
    })
  },
  noteImage(e) {
    const {
      file
    } = e.detail;
    // console.log(file);
    let self = this

    wx.uploadFile({
      url: 'https://lionely.top:444/upload.php', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        let r = JSON.parse(res.data);
        const {
          fileList = []
        } = self.data;
        fileList.push({
          ...file,
          url: r.path
        });
        self.setData({
          fileList,
          noteImage: r.path
        });
      },
    });
  },
  onSelect(e) {

    this.showAction()
    this.setData({
      noteType: e.detail.name
    })
    console.log(this.data.noteType);
  },
  publish() {
    let {
      id,
      nickname
    } = wx.getStorageSync('userInfo')
    request('post', '/users/publishNote', {
      noteTitle: this.data.noteTitle,
      noteImage: this.data.noteImage,
      noteType: this.data.noteType,
      noteContent: this.data.noteContent,
      userId: id,
      nickname: nickname
    }).then(res => {
      setTimeout(() => {
        toast(res.data.mess)
      }, 5000)
      if (res.data.code == 200) {
        wx.navigateTo({
          url: `/pages/noteList/noteList?title=${this.data.noteType}`,
        })
      }
    })
  },
  showAction() {
    this.setData({
      show: !this.data.show
    })
  }

})